package com.liy.framework.web.rs.service;

import com.liy.framework.common.dao.IBaseDao;
import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.vo.PageVO;

import java.io.Serializable;
import java.util.List;

public abstract class BaseRSServiceImpl<T, PK extends Serializable> implements IBaseRSService<T, PK> {


    public abstract IBaseDao<T, PK> getDao();

    public ResultVO add(T t) throws Exception {
        int i = this.getDao().insertSelective(t);
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(i > 0);
        resultVO.setMessage("新增成功");
        return resultVO;
    }

    public ResultVO update(T t) throws Exception {
        int i = this.getDao().updateByPrimaryKeySelective(t);
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(i > 0);
        resultVO.setMessage("保存成功");
        return resultVO;
    }

    public ResultVO delete(PK id) throws Exception {
        this.getDao().deleteByPrimaryKey(id);
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setMessage("删除成功");
        return resultVO;
    }

    public T findById(PK id) throws Exception {
        return this.getDao().selectByPrimaryKey(id);
    }

    public List<T> findList(T t) throws Exception {
        return this.getDao().selectList(t);
    }

    public List<T> findByPage(T t, Integer pageSize, Integer pageNo) throws Exception {
        return this.getDao().selectByPage(t, new PageVO(pageNo, pageSize));
    }
}
